package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 业务实现
 */
@SuppressWarnings("all")
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    /**
     * 分页条件查询频道信息
     *
     * @param channelDTO
     * @return
     */
    @Override
    public ResponseResult<AdChannel> findChannelByPage(ChannelDTO channelDTO) {

        //判断参数是否为空
        if (channelDTO == null) {
            //返回错误信息  声明没有参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断分页参数是否为空
        channelDTO.checkParam();

        //走到这步说明channelDTO不为空
        //创建查询
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        //判断频道名称是否为空
        if (StringUtils.isNotBlank(channelDTO.getName())) {
            //根据频道名模糊查询
            wrapper.like(AdChannel::getName, channelDTO.getName());
        }
        //判断状态码
        if (channelDTO.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus, channelDTO.getStatus());
        }
        //进行排序
        wrapper.orderByAsc(AdChannel::getOrd);


        Page<AdChannel> page = new Page<>(channelDTO.getPage(), channelDTO.getSize());
        //实现分页查询
        IPage<AdChannel> iPage = this.page(page, wrapper);
        return new PageResponseResult(channelDTO.getPage(), channelDTO.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 新增频道
     *
     * @param channel 频道对象
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel channel) {
        //判断名称是否为空,长度不能大于10
        String name = channel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        //判断长度
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能超过10");
        }

        //判断频道名称是否在数据表存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        //判断是否存在
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称不能重复");
        }
        //走到这步说明满足条件,将数据存入表中
        //先设置创建时间
        channel.setCreatedTime(new Date());
        //保存
        this.save(channel);
        //返回成功保存
        return ResponseResult.okResult();
    }

    /**
     * 修改频道信息
     *
     * @param channel 修改后的频道信息
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        //判断id是否为空
        if (channel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "没有该频道,修改失败!");
        }
        //判断名字是否为空
        String newName = channel.getName();
        if (StringUtils.isBlank(newName)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        //判断名字长度是否满足条件
        if (newName.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能超过10");
        }
        AdChannel adChannel = getById(channel.getId());
        //判断在数据表中是否存在该名字
        if (!channel.getName().equals(adChannel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newName));
            if (count > 0) {
                //大于0则说明该频道已存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已经存在了!");
            }
        }
        //进行修改
        updateById(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 根据频道id删除频道信息
     *
     * @param id 频道id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //判断频道id是否为空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "没有该频道!");
        }

        //判断当前频道是否存在
        AdChannel channel = getById(id);
        if (channel == null) {
            //如果为null说明该频道不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "该频道不存在!删除失败!");
        }
        //判断该频道是否在启用状态,启用状态不能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "启用状态下无法删除频道!");
        }
        //删除频道
        removeById(id);
        //返回删除成功
        return ResponseResult.okResult();
    }
}
